/*
    Copyright(C) 2010 Ying-Chun Liu(PaulLiu). All rights reserved.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

package org.debian.paulliu.utils;

public class SSLSocketCipherSuiteChooser {

    static private String[] blackList = {
	"NULL", "DES40", "DES", "3DES", "MD5", "RENEGOTIATION"
    };

    static private String[] blackList2 = {
	"RC4_40"
    };

    static private boolean isInStringArray(String s, String [] a) {
	int i;
	for (i=0; i<a.length; i++) {
	    if (s == a[i]) {
		return true;
	    }
	}
	return false;
    }

    static private boolean isInStringArrayIgnoreCase(String s, String [] a) {
	int i;
	for (i=0; i<a.length; i++) {
	    if (s.equalsIgnoreCase(a[i])) {
		return true;
	    }
	}
	return false;
    }

    static private String[] pickAnonymousCipherSuites(String [] cipherSuites) {
	int i,j;
	java.util.Vector<String> results = new java.util.Vector<String> ();
	String[] ret = null;
	for (i=0; i<cipherSuites.length; i++) {
	    String[] ciphers = cipherSuites[i].split("_");
	    boolean pickFlag=true;

	    for (j=0; pickFlag && j<ciphers.length; j++) {
		if (isInStringArrayIgnoreCase(ciphers[j], blackList)) {
		    pickFlag=false;
		}
	    }

	    for (j=0; pickFlag && j<blackList2.length; j++) {
		if (cipherSuites[i].contains(blackList2[j])) {
		    pickFlag=false;
		}
	    }
	    if (pickFlag) {
		results.add(cipherSuites[i]);
	    }
	}

	ret = results.toArray(new String[1]);
	return ret;
    }

    static public String[] getAnonymousCipherSuites(javax.net.ssl.SSLSocket sslSocket) {
	String[] supportedCipherSuites = sslSocket.getSupportedCipherSuites();

	return pickAnonymousCipherSuites(supportedCipherSuites);
    }

    static public String[] getAnonymousCipherSuites(javax.net.ssl.SSLSocketFactory sslSocketFactory) {
	String[] supportedCipherSuites = sslSocketFactory.getSupportedCipherSuites();

	return pickAnonymousCipherSuites(supportedCipherSuites);
    }

    static public String[] getAnonymousCipherSuites() {
	javax.net.ssl.SSLSocketFactory sslfactory;
	sslfactory = javax.net.ssl.HttpsURLConnection.getDefaultSSLSocketFactory();

	return getAnonymousCipherSuites(sslfactory);
    }

    static public void main (String[] args) {
	if (args.length == 0) {
	    System.out.println("Support commands: print_all_algorithm|print_selected_algorithm|print_default_algorithm");
	    System.exit(0);
	}
	if (args[0].equals("print_all_algorithm")) {
	    javax.net.ssl.SSLSocketFactory sslfactory;
	    sslfactory = javax.net.ssl.HttpsURLConnection.getDefaultSSLSocketFactory();
	    String[] supportedCipherSuites = sslfactory.getSupportedCipherSuites();
	    for (int i=0; i<supportedCipherSuites.length; i++) {
		System.out.println(supportedCipherSuites[i]);
	    }
	}
	if (args[0].equals("print_selected_algorithm")) {
	    String[] mySuites = getAnonymousCipherSuites();
	    for (int i=0; i<mySuites.length; i++) {
		System.out.println(mySuites[i]);
	    }
	}
	if (args[0].equals("print_default_algorithm")) {
	    javax.net.ssl.SSLSocketFactory sslfactory;
	    sslfactory = javax.net.ssl.HttpsURLConnection.getDefaultSSLSocketFactory();
	    String[] defaultCipherSuites = sslfactory.getDefaultCipherSuites();
	    for (int i=0; i<defaultCipherSuites.length; i++) {
		System.out.println(defaultCipherSuites[i]);
	    }
	}
    }

}